<?php
/**
 * TeacherAction 
 * 友情链接管理模块
 */
class LinkAction extends Action{
	/**
	 * __construct 
	 * 构造函数,加入登陆验证,防止非法登陆
	 * @access public
	 * @return void
	 */
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION['admin_id'])){
			header("Content-Type:text/html; charset=utf-8");
			$this->redirect('Index/login',null,3,"非法登录,3秒后为你跳转到登录页面..");
		}
	}
	/**
	 * index 
	 * 列表显示当前的链接
	 * @access public
	 * @return void
	 */
	public function index(){
		$linkModel = new Model('Link');
		$collegeLinks = $linkModel->where("type=0")->select();
		$departmentLinks = $linkModel->where("type=1")->select();
		$otherLinks = $linkModel->where("type=2")->select();
		$this->assign('collegeLinks',$collegeLinks);
		$this->assign('departmentLinks',$departmentLinks);
		$this->assign('otherLinks',$otherLinks);
    	$this->display();
    }
    /**
     * add 
     * 添加一个链接
     * @access public
     * @return void
     */
    public function add(){
		if(isset($_POST['name'])){
			$linkModel = new Model('Link');
			$linkModel->create();
			if($linkModel->add()){
				$this->redirect('index');
			}
		}
		header("Content-Type:text/html; charset=utf-8");
		$this->redirect('Index/login',null,3,"非法操作,3秒后为你跳转到登录页面..");
    }

	/**
	 * modify 
	 * 修改链接
	 * @access public
	 * @return void
	 */
	public function modify(){
		// var_dump($_POST);
		if(isset($_POST['link_id'])){
			$linkModel = new Model('Link');
			$linkModel->create();
			$linkModel->save();
			// $this->display();
			$this->redirect('index');
		}
		header("Content-Type:text/html; charset=utf-8");
		$this->redirect('Index/login',null,3,"非法操作,3秒后为你跳转到登录页面..");
	}

	/**
	 * delete 
	 * 删除一个链接
	 * @access public
	 * @return void
	 */
	public function delete(){
		if(isset($_GET['link_id']) and preg_match('/\d+/',$_GET['link_id'])){
			$linkModel = new Model('Link');
			if($linkModel->where("link_id={$_GET['link_id']}")->delete()){
				$this->redirect('index');
			}
		}
		header("Content-Type:text/html; charset=utf-8");
		$this->redirect('Index/login',null,3,"非法操作,3秒后为你跳转到登录页面..");
	}
}
?>
